package com.dark;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by tengxue on 16-6-21.
 */
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class RedisMapDemo extends AbstractJUnit4SpringContextTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String  MAP_NAME="map_demo";

    @Test
    public void testAddElement() {
        Map<String,String> elements=new HashMap<String, String>();
        elements.put("a","1");
        elements.put("b","2");
        elements.put("c","3");
        elements.put("d","4");
        elements.put("e","5");
        stringRedisTemplate.opsForHash().putAll(MAP_NAME, elements);
        stringRedisTemplate.expire(MAP_NAME, 5, TimeUnit.MINUTES);

        String element=(String)stringRedisTemplate.opsForHash().get(MAP_NAME,"a");
        System.out.println("the element is :" + element);
    }

    @Test
    public void testGetAllElement() {
        Map result=stringRedisTemplate.opsForHash().entries(MAP_NAME);
        System.out.println("the result is :" + result);
    }



}
